package ro.jsug.cssoup;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import java.util.*;
import java.io.*;

import javax.swing.ImageIcon;
import java.awt.image.*;
import java.awt.*;
import java.awt.Graphics;
import javax.imageio.ImageIO;

/**
 * Concat images and generates CSS.
 * @goal generateSprites
 */
public class SpritesMojo extends AbstractMojo{

	/**
     * Enum of selectors.
     *
     * @parameter
     */
    private Properties parts;
	
    /**
     * Output file name for CSS.
     *
     * @parameter
     */
    private String cssOutputFile;

    /**
     * Sprite image name (content will be generated).
     *
     * @parameter
     */
    private String spriteImage;
       
    
    private String getRule(String selector, String backgroundFile, Integer width, Integer height, Integer cssPosX, Integer cssPosY){    	
    	return selector + " {height: " + height + "px;width:" + width + "px;background:url('" + backgroundFile + "') " + cssPosX +"px " + cssPosY + "px no-repeat;}";
    }
    
    private StringBuilder concatImage(Properties parts, String outImageType, String outImageName) throws IOException{
    	Vector icons = new Vector();
    	
    	//ImageIcon[] icons;
    	
    	int width = 0;
    	int height = 0;
    	
    	for (int i=0; i< parts.values().size(); i++){
    		ImageIcon icon = new ImageIcon((String)parts.values().toArray()[i]);
    		
    		if (icon.getIconWidth() > width){
    			width = icon.getIconWidth();
    		}
    		
    		height += icon.getIconHeight();
    		
    		icons.add(icon);
    	}
    	
        BufferedImage image = new BufferedImage(width, height, Image.SCALE_DEFAULT);
        Graphics graphics = image.getGraphics();

        graphics.setColor(Color.white);
        
        graphics.fillRect(0,0, width, height);
        
        Iterator component = icons.iterator();
        
        int heightCursor = 0;

		StringBuilder contents = new StringBuilder(); 
		String line = null;        
        
		int i = 0;
        while (component.hasNext()){
        	ImageIcon sprite = (ImageIcon) component.next();
        	graphics.drawImage(sprite.getImage(), 0, heightCursor, sprite.getIconWidth(), sprite.getIconHeight(), null);
        	String key = (String)parts.keySet().toArray()[i++];
			line = getRule(key, outImageName, sprite.getIconWidth(), sprite.getIconHeight(), 0, -heightCursor);
        	heightCursor += sprite.getIconHeight();
        	
			contents.append(line);
			contents.append(System.getProperty("line.separator"));
        }
        
        
        ImageIO.write(image, outImageType, new File(outImageName));    	

    	return contents;
    	/*
        ImageIcon img1 = new ImageIcon(firstImg);
        ImageIcon img2 = new ImageIcon(secondImg);

        BufferedImage image = new BufferedImage(Math.max(img1.getIconWidth(), img2.getIconWidth()), img1.getIconHeight() +  img2.getIconHeight(), Image.SCALE_DEFAULT);
        Graphics graphics = image.getGraphics();

        graphics.setColor(Color.white);
        graphics.fillRect(0,0, Math.max(img1.getIconWidth(), img2.getIconWidth()),
                img1.getIconHeight() +  img2.getIconHeight());
        graphics.drawImage(img1.getImage(), 0,0, img1.getIconWidth(), img1.getIconHeight(), null);
        graphics.drawImage(img2.getImage(), 0,img1.getIconHeight() , img2.getIconWidth(), img2.getIconHeight(), null);

        ImageIO.write(image, outImageType, new File(outImageName));
        */    	
    }
    
    public void execute() throws MojoExecutionException
    {
    	
        getLog().info("Writing CSS Rules");

		/*Enumeration enum_ = parts..keys () ;
		
		Vector imageNames = new Vector();
		
		while ( enum_.hasMoreElements (  )  )  {  
			//getLog().info( "Element is..." + myProperties.get ( enum_.nextElement (  )  )  ) ;
			String key = (String) enum_.nextElement();
			String value = (String) parts.get(key);
			
			imageNames.add(value);			
        }*/
		
    	try{
    		StringBuilder contents = concatImage(parts, "jpg", spriteImage);
    						
    		//use buffering
    		String aFile = cssOutputFile;		
	
			Writer output = new BufferedWriter(new FileWriter(aFile));
			//FileWrite always assumes default encoding is OK!       
			output.write( contents.toString() );   
			output.close();  
		} catch(IOException e){
			;
		}
		finally {
			;
		}				
	}
}